﻿using NewLife.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfBill.Data;

namespace WpfBill.Controls
{
    /// <summary>
    /// UCBill.xaml 的交互逻辑
    /// </summary>
    public partial class UCBill : UserControl
    {
        /// <summary>
        /// 项目信息
        /// </summary>
        private Project project = null;
        /// <summary>
        /// 项目集合
        /// </summary>
        private IList<Project> Projects = null;

        public UCBill()
        {
            InitializeComponent();
            Init();
        }
        private void Init()
        {
            Projects = Project.FindAll();
            if (Projects != null)
            {
                foreach (var item in Projects)
                {
                    cmbName.Items.Add(new ComboBoxItem() { Content = item.Name });
                }
                // cmbName.ItemsSource = Projects.Select(p => p.Name);
            }
            ucPage.PageChanged += UcPage_PageChanged;
            ucPage.PageSizeChanged += UcPage_PageSizeChanged;

        }
        /// <summary>
        /// 显示项目下信息
        /// </summary>
        /// <param name="project"></param>
        public void Show(Project project)
        {
            this.project = project;

            QueryData(0);
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rp"></param>
        private void QueryData(int page)
        {
            try
            {
                lbProBalance.Content = "￥" + project.Balance;
                int rp = ucPage.PageSize;
                long count = Business.FindCount();
                ucPage.Count = (int)count;
                PageParameter pageParam = new PageParameter() { PageIndex = page, PageSize = rp };

                XCode.Expression where = new XCode.Expression();
                string enterName = cmbEnterpriseName.Text.Trim();
                if (project != null)
                    where &= Business._.ProjectID == (project.ID);
                if (!string.IsNullOrEmpty(enterName))
                    where &= Business._.EnterpriseName.Contains(enterName);

                var list = Business.FindAll(where, pageParam);
                this.dgData.ItemsSource = list;
            }
            catch (Exception ex)
            {

            }
        }


        private void BtnAdd_Click(object sender, RoutedEventArgs e)
        {
            GUI.WinBill win = new GUI.WinBill();
            win.ShowDialog(project);
            QueryData(ucPage.Page);
        }

        private void UcPage_PageChanged(object sender, int page, int rp)
        {
            QueryData(page);
        }

        private void UcPage_PageSizeChanged(object sender, int page, int rp)
        {
            QueryData(page);
        }

        private void dgBtnEdit_Click(object sender, RoutedEventArgs e)
        {

        }

        private void dgBtnDel_Click(object sender, RoutedEventArgs e)
        {

            try
            {
                var business = dgData.SelectedItem as Business;
                Project project = Project.FindByID(business.ProjectID);
                using (var tran = Project.Meta.CreateTrans())
                {
                    if (business.LoanType == 1)
                    {
                        project.Balance -= business.Amount;
                    }
                    else
                    {
                        project.Balance += business.Amount;
                    }
                    if (business != null)
                        business.Delete();
                    project.Save();
                    tran.Commit();
                }
                QueryData(ucPage.Page);
            }
            catch (Exception ex)
            {

            }
        }

        private void dgBtnProjectView_Click(object sender, RoutedEventArgs e)
        {

        }

        private void dgBtnView_Click(object sender, RoutedEventArgs e)
        {

        }
        private void CmbName_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {

            int index = cmbName.SelectedIndex;
            if (index > -1)
            {
                project = Projects[index];
                QueryData(0);
            }
        }

        private void CmbEnterpriseName_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            QueryData(0);
        }

        private void CmbName_SearchTextChanged(object sender, Panuon.UI.Silver.Core.SearchTextChangedEventArgs e)
        {
            try
            {
                ComboBox comboBox = sender as ComboBox;
                if (comboBox == null)
                    return;
                foreach (ComboBoxItem item in comboBox.Items)
                {
                    item.Visibility = item.Content.ToString().Contains(e.Text) ? Visibility.Visible : Visibility.Collapsed;
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void CmbEnterpriseName_SearchTextChanged(object sender, Panuon.UI.Silver.Core.SearchTextChangedEventArgs e)
        {

            try
            {
                ComboBox comboBox = sender as ComboBox;
                if (comboBox == null)
                    return;
                foreach (ComboBoxItem item in comboBox.Items)
                {
                    item.Visibility = item.Content.ToString().Contains(e.Text) ? Visibility.Visible : Visibility.Collapsed;
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void DgData_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            try
            {
                var business = e.Row.Item as Business;
                int index = e.Column.DisplayIndex;
                string header = ((System.Windows.Data.Binding)((System.Windows.Controls.DataGridBoundColumn)e.Column).Binding).Path.Path;
                string value = (e.Column.GetCellContent(e.Row) as TextBox).Text;
                if (business[header].ToString() != value)
                {
                    decimal amount = 0;
                    if (!decimal.TryParse(value, out amount))
                        return;
                    Project project = Project.FindByID(business.ProjectID);
                    using (var tran = Business.Meta.CreateTrans())
                    {
                        if (header == Business.__.Amount)
                        {
                            if (business.LoanType == 1)
                            {
                                project.Balance += (amount - business.Amount);
                            }
                            else
                            {
                                project.Balance -= (amount - business.Amount);
                            }
                            project.Save();
                        }

                        business[header] = value;
                        business.Save();
                        tran.Commit();
                    }
                    lbProBalance.Content = "￥" + project.Balance;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }
    }
}
